package org.hbnu;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class BookAdmin {
    //图书类对象集合
    public static List<Book> bookList = new ArrayList<>();

    //输入对象
    public static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) throws Exception {

        System.out.println("-----------图书管理系统v1.0--------------------");

        //创新对象
        Book book1 = new Book(1, "红楼梦", 12.5f);
        Book book2 = new Book(2, "西游记", 17.5f);
        Book book3 = new Book(3, "水浒传", 13.5f);

        //把对象加入到集合
        bookList.add(book1);
        bookList.add(book2);
        bookList.add(book3);


        //查看
        while (true) {
            //进入菜单
            System.out.print("1.查看\t");
            System.out.print("2.查找\t");
            System.out.print("3.增加\t");
            System.out.print("4.删除\t");
            System.out.print("5.修改\t");
            System.out.print("6.保存\t");
            System.out.println("7.退出\t");
            System.out.println("请输入：");
            //选择功能
            int k = scanner.nextInt();
            if (k == 1) show();
            if (k == 2) search();
            if (k == 3) add();
            if (k == 4) delete();
            if (k == 7) {
                System.out.println("欢迎下次使用.");
                break;
            }
        }


    }

    //查看
    public static void show() {
        //显示
        for (int i = 0; i <= bookList.size() - 1; i = i + 1) {
            Book book = bookList.get(i);
            System.out.println(book.id + " " + book.price + " " + book.name);
        }
    }

    //查找
    public static void search() {
        System.out.print("要查找的名字：");
        String name = scanner.next();

        //查找的序号
        int index = -1;
        for (int i = 0; i <= bookList.size() - 1; i = i + 1) {
            Book book = bookList.get(i);
            if (book.getName().equals(name)) {
                index = i;
                break;
            }
        }
        //判断查找失败
        if (index == -1) {
            System.out.println("没有此书！");
            return;
        }

        //查找成功
        Book book = bookList.get(index);
        System.out.println(book.id + " " + book.price + " " + book.name);
    }

    //增加
    public static void add() {
        System.out.print("名字：");
        String name = scanner.next();
        System.out.print("价格：");
        float price = scanner.nextFloat();
        Book book = new Book(bookList.size() + 1, name, price);
        bookList.add(book);
    }

    //删除
    public static void delete() {
        System.out.print("要查找的编号：");
        int id = scanner.nextInt();

        //查找的序号
        int index = -1;
        for (int i = 0; i <= bookList.size() - 1; i = i + 1) {
            Book book = bookList.get(i);
            if (book.getId() == id) {
                index = i;
                break;
            }
        }
        //判断查找失败
        if (index == -1) {
            System.out.println("没有此书！");
            return;
        }
        //再删除
        bookList.remove(index);
        System.out.println("删除成功！");

    }
}